package com.ws.shop.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.ws.shop.bean.Page;
import com.ws.shop.dao.PunchDao;
import com.ws.shop.entity.Punch;

@Repository(value="punchDao")
public class PunchDaoImpl  extends BaseDaoImpl implements PunchDao {

	protected Object rowMapper(ResultSet rs) throws SQLException {
		return null;
	}

	/**
	 * 是否打卡
	 * @param uid
	 * @return
	 */
	public Object isPunch(long uid){
		String s = "select id,uid,name,addtime from jvf_punch where uid = ? and (addtime >= date_format(now(),'%Y-%m-%d %H:%i:%s') and addtime <= date_add(now(),interval 1 day))";
		return this.queryForObject(s, new Object[]{new Long(uid)}, new RowMapper<Punch>(){
			public Punch mapRow(ResultSet rs, int value) throws SQLException {
				Punch punch = new Punch(); 
				punch.setId(rs.getLong("id"));
				punch.setUid(rs.getInt("uid"));
				punch.setAddtime(rs.getTimestamp("addtime"));
				punch.setName(rs.getString("name"));
				return punch;
			}
		});
	}
	
	/**
	 * 打卡
	 * @param uid
	 * @param name
	 * @return
	 */
	public int punching(long uid,String name){
		String s = "insert into jvf_punch(uid,name,addtime) values(?,?,now())";
		return this.update(s, new Object[]{new Long(uid),name});
	}

	final String findPunchListByPage_count = "select count(1) from jvf_punch";
	final String findPunchListByPage_list = "select id,uid,name,addtime from jvf_punch";
	final String findPunchListByPage_order = " order by id desc";
	public Page findPunchListByPage(int pageno, int pagesize, String start,
			String end, String name) {
		StringBuilder query = new StringBuilder(" where 1=1");
		if(name != null){
			query.append(" and name='").append(name).append("'");
		}
		query.append(" and addtime<='").append(end).append(" 23:59:59'")
			.append(" and addtime>='").append(start).append(" 00:00:00'");
		return this.findPage(pageno, pagesize, new StringBuilder(findPunchListByPage_count).append(query).toString(),
				new StringBuilder(findPunchListByPage_list).append(query).append(findPunchListByPage_order));
	}
}
